package com.ivncorp.evnproject.data;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.ivncorp.evnproject.model.ObjectTuTT;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

public class TuTTDao extends AbstractDao<ObjectTuTT, Long> {

	public static final String TABLENAME = "TUTT";

	public static class Properties {
		public final static Property Id = new Property(0, Long.class, "id",
				true, "_id");
		public final static Property uploaded = new Property(1, Integer.class,
				"uploaded", false, "UPLOADED");
		public final static Property image = new Property(2, String.class,
				"image", false, "IMAGE");
		public final static Property code = new Property(3, String.class,
				"code", false, "CODE");
		public final static Property name = new Property(4, String.class,
				"name", false, "NAME");
		public final static Property timestamp = new Property(5, String.class,
				"timestamp", false, "TIMESTAMP");

	};

	public TuTTDao(DaoConfig config) {
		super(config);
	}

	public TuTTDao(DaoConfig config, DaoSession daoSession) {
		super(config, daoSession);
	}

	public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
		String constraint = ifNotExists ? "IF NOT EXISTS " : "";
		db.execSQL("CREATE TABLE " + constraint + "'TUTT' ("
				+ "'_id' INTEGER PRIMARY KEY ," + "'UPLOADED' INTEGER,"
				+ "'IMAGE' TEXT," + "'CODE' TEXT," + "'NAME' TEXT,"
				+ "'TIMESTAMP' TEXT);");
	}

	public static void dropTable(SQLiteDatabase db, boolean ifExists) {
		String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'TUTT'";
		db.execSQL(sql);
	}

	@Override
	protected void bindValues(SQLiteStatement stmt, ObjectTuTT entity) {
		stmt.clearBindings();

		long id = entity.getId_object();
		if (id > -1) {
			stmt.bindLong(1, id);
		}

		byte uploaded = entity.getUploaded();
		if (uploaded > -1) {
			stmt.bindLong(2, uploaded);
		}

		String image = entity.getImage_1();
		if (image != null) {
			stmt.bindString(3, image);
		}

		String code = entity.getCode();
		if (code != null) {
			stmt.bindString(4, code);
		}

		String name = entity.getName();
		if (name != null) {
			stmt.bindString(5, name);
		}

		String timestamp = entity.getTimestamp();
		if (timestamp != null) {
			stmt.bindString(6, timestamp);
		}

	}

	@Override
	protected Long getKey(ObjectTuTT entity) {
		if (entity != null) {
			return entity.getId_object();
		} else {
			return null;
		}
	}

	@Override
	protected boolean isEntityUpdateable() {
		return true;
	}

	@Override
	protected ObjectTuTT readEntity(Cursor cursor, int offset) {
		ObjectTuTT entity = new ObjectTuTT(
				cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0),
				cursor.isNull(offset + 1) ? null : (byte) cursor
						.getLong(offset + 1),
				cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2),
				cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3),
				cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4),
				cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
		return entity;
	}

	@Override
	protected void readEntity(Cursor cursor, ObjectTuTT entity, int offset) {
		entity.setId_object(cursor.isNull(offset + 0) ? null : cursor
				.getLong(offset + 0));
		entity.setUploaded(cursor.isNull(offset + 1) ? null : (byte) cursor
				.getLong(offset + 1));
		entity.setImage_1(cursor.isNull(offset + 2) ? null : cursor
				.getString(offset + 2));
		entity.setCode(cursor.isNull(offset + 3) ? null : cursor
				.getString(offset + 3));
		entity.setName(cursor.isNull(offset + 4) ? null : cursor
				.getString(offset + 4));
		entity.setTimestamp(cursor.isNull(offset + 5) ? null : cursor
				.getString(offset + 5));
	}

	@Override
	protected Long readKey(Cursor cursor, int offset) {
		return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
	}

	@Override
	protected Long updateKeyAfterInsert(ObjectTuTT entity, long rowId) {
		entity.setId_object(rowId);
		return rowId;
	}

}
